﻿using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

namespace Sidvall.Reporting
{
    public class ReportManager
    {
        #region Public Members

        public Sidvall.Reporting.IReport Report { get; set; }

        #region ExportAsync

        public async Task<Stream> ExportAsync(string reportPath, RenderingFormat format, DataSource dataSource)
        {
            using (var reportStream = await Sidvall.SystemContext.Current.SystemStorageManager.OpenFileAsync(reportPath, Sidvall.IO.FileMode.Open, Sidvall.IO.FileAccess.Read).ConfigureAwait(false))
            {
                return await ExportAsync(reportStream, format, dataSource).ConfigureAwait(false);
            }
        }
        public async Task<Stream> ExportAsync(string reportPath, RenderingFormat format, IEnumerable<DataSource> dataSources, IEnumerable<Parameter> parameters)
        {
            using (var reportStream = await Sidvall.SystemContext.Current.SystemStorageManager.OpenFileAsync(reportPath, Sidvall.IO.FileMode.Open, Sidvall.IO.FileAccess.Read).ConfigureAwait(false))
            {
                return await ExportAsync(reportStream, format, dataSources, parameters).ConfigureAwait(false);
            }
        }
        public async Task<Stream> ExportAsync(Stream reportStream, RenderingFormat format, DataSource dataSource)
        {
            var dataSources = new System.Collections.Generic.List<DataSource>();
            dataSources.Add(dataSource);
            return await ExportAsync(reportStream, format, dataSources, null).ConfigureAwait(false);
        }
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1822:MarkMembersAsStatic")]
        public async Task<Stream> ExportAsync(Stream reportStream, RenderingFormat format, IEnumerable<DataSource> dataSources, IEnumerable<Parameter> parameters)
        {
            return await this.Report.ExportAsync(reportStream, format, dataSources, parameters).ConfigureAwait(false);
        }

        #endregion

        #region ToString

        public override string ToString()
        {
            return "ReportManager";
        }

        #endregion

        #endregion
        #region Constructors

        public ReportManager(Sidvall.Reporting.IReport report)
        {
            this.Report = report;
        }

        #endregion
    }
}
